<%@ page import="com.tianxiabuyi.model.base.TXBYObject" %>
<%@ page pageEncoding="UTF-8"%>
<%!
	public final static Integer REGISTERORDER = 1;  // 预约提醒
	public final static Integer FOLLOWUPVISIT = 2;	// 随访提醒
	public final static Integer SERVERORDER = 3;    // 服务提醒
	public final static Integer PROGRAM = 4;		// 日程提醒
	public final static Integer DOCTORWORD = 5;		// 医嘱提醒
	public final static Integer AFFAIRWORD = 6;		// 事务提醒
	public final static Integer SYSTEMINFO = 7;		// 系统提醒
	public final static Integer QUESTINFO = 8;		// 圈子提醒
	public final static Integer DOCTORATTENTION = 9;		// 医生关注提醒
	public  String getWeekday(String source) {
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return DateUtil.formatDateByFormat(date, "E");
	}
	public boolean createMsg(TXBYObject obj, Integer uid) {
		if (obj instanceof Information) {
			Information info = (Information)obj;
			info.setUid(uid);
			Integer id = (Integer)DBTools.db("basedb").addItem(info);
			return !isEmpty(id);
		}
		String title = "";
		String sumary = "";
		String content = "";
		Information info = new Information();
		info.setUid(uid);
		info.setHospital(obj.getHospital());
		info.setOid(Tools.getDesc(obj.getId(), "-1"));
		if (obj instanceof RegisterOrder) {     // 预约挂号
			RegisterOrder ro = (RegisterOrder)obj;
			Integer flag = ro.getRegisterStatus();
			info.setCategory(1);
			if (flag == 1){
				title = "预约通知";
				sumary = "预约通知";
				content = Tools.getDesc(ro.getPatientName(), "") + ":请于 " + Tools.getDesc(ro.getDate(), "") + " " + getWeekday(ro.getDate()) + (Tools.getDesc(ro.getHalf(), "").equals("1") ? "上午" : "下午") + Tools.getDesc(ro.getJson("start_time"), "") + "-" + Tools.getDesc(ro.getJson("end_time"), "") + "至医院的一卡通终端机上取号。您预约的专家为 " + Tools.getDesc(ro.getDeptName(), "") + " " + Tools.getDesc(ro.getDoctorName(), "") + "，您的就诊序号为11。祝您早日康复";
			} else {
				title = "取消预约通知";
				sumary = "取消预约通知";
				String json = Tools.getDesc(ro.getJsonString(), "");
				Map<String, String> _map = JSONUtil.fromJson(json, new TypeReference<Map<String, String>>(){});
				content = "您已取消" + Tools.getDesc(ro.getPatientName(), "") + "于" + Tools.getDesc(ro.getDate(), "") + (Tools.getDesc(ro.getHalf(), "").equals("1") ? "上午" : "下午") +Tools.getDesc(_map.get("start_time"), "") + "的预约";
			}
		} else if(obj instanceof Order) {     // 预约服务
			Order o = (Order)obj;
			Integer flag = o.getState();
			info.setCategory(3);
			if (flag == 5 || flag == 4) {
				title = "取消服务通知";
				sumary = "取消预约通知";
				String half = Tools.getDesc(o.getHalf(), "");
				half = "1".equals(half) ? "上午" : "2".equals(half) ? "下午" : "";
				String date = Tools.getDesc(o.getStartTime(), "") +"-" + Tools.getDesc(o.getEndTime(), "");
				String week = "";
				if (o.getExecTime() != null) {
					date = DateUtil.formatDate(new Date(o.getExecTime()));
					week =  getWeekday(date);
				}
				content = Tools.getDesc(o.getUserName(), "") + ",您预约的" + date + " " + week + half + ""+ Tools.getDesc(o.getDoctorName(), "") + "的" + Tools.getDesc(o.getTitle(), "") + "服务已被取消";
			} else if (flag == 1) {
				title = "服务预约通知";
				sumary = "服务预约通知";
				String half = Tools.getDesc(o.getHalf(), "");
				half = "1".equals(half) ? "上午" : "2".equals(half) ? "下午" : "";
				String date = Tools.getDesc(o.getStartTime(), "") +"-" + Tools.getDesc(o.getEndTime(), "");
				String week = "";
				if (o.getType() == 2) {
					date = DateUtil.formatDate(new Date(o.getExecTime()));
					week = getWeekday(date);
				}
				content = Tools.getDesc(o.getUserName(), "") + ",您预约了" + date + " " + week + half + ""+ Tools.getDesc(o.getDoctorName(), "") + "的" + Tools.getDesc(o.getTitle(), "") + "服务";
			}
		} else {
			return false;
		}
		info.setTitle(title);
		info.setSumary(sumary);
		info.setContent(content);
		Integer id = (Integer)DBTools.db("basedb").addItem(info);
		return !isEmpty(id);
	}

	public void createMsg(Integer uid, Integer hospital, String title, String summary, String content, Integer category) {
		createMsg( uid,  hospital,  title,  summary,  content,  category, "-1");
	}

	public void createMsg(Integer uid, Integer hospital, String title, String summary, String content, Integer category, String oid) {
		createMsg(new ArrayList<Integer>(Arrays.asList(uid)),  hospital,  title,  summary,  content,  category, oid);
	}

	public void createMsg(List<Integer> uids, Integer hospital, String title, String summary, String content, Integer category, String oid) {
		List<Information> informations = new ArrayList<Information>();
		for (Integer uid:uids) {
			Information info = new Information();
			info.setUid(uid);
			info.setTitle(title);
			info.setSumary(summary);
			info.setContent(content);
			info.setCategory(category);
			info.setHospital(hospital);
			info.setOid(oid);
			informations.add(info);
		}
		DBTools.db("basedb").addItem(informations);
	}
%>
